package com.noahplat.sms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.*;
import com.noahplat.sms.feign.OtherSmsFeign;
import com.noahplat.sms.mapper.SmsMapper;
import com.noahplat.sms.mapper.WhiteipMapper;
import com.noahplat.sms.pojo.Sms;
import com.noahplat.sms.pojo.Whiteip;
import com.noahplat.sms.service.SmsService;
import com.noahplat.sms.service.WhiteIpService;
import com.noahplat.user.feign.UserFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: WhiteIpServiceImpl
 * @Description: 白名单
 * @Author  宋彦磊
 * @Date 2021/2/25
 * @Version 1.0
 */
@Service
@GlobalTransactional
public class WhiteIpServiceImpl implements WhiteIpService {

    @Autowired
    private WhiteipMapper whiteipMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 添加白名单
     * @param whiteip
     * @return
     */
    @Transactional
    @Override
    public Result addWhiteIp(Whiteip whiteip){
        Example example = new Example(Whiteip.class);
        example.createCriteria().andEqualTo("whiteip",whiteip.getWhiteip());
        Whiteip whiteip1 = whiteipMapper.selectOneByExample(example);
        if(!ObjectUtils.isEmpty(whiteip1)){
            return new Result(false,StatusCode.ERROR,"已存在该白名单");
        }
        int res = whiteipMapper.insertSelective(whiteip);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.WHITEIPDIR.getCode()+whiteip.getWhiteip(),whiteip);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"插入内存出错");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 获取白名单
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getWhiteIp(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Whiteip> list = whiteipMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Whiteip> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /**
     * 删除白名单
     * @param id
     * @return
     */
    @Override
    public Result delWhiteIp(Long id){
        Whiteip whiteip = whiteipMapper.selectByPrimaryKey(id);
        if(ObjectUtils.isEmpty(whiteip)){
            return new Result(false,StatusCode.ERROR,"查不到数据");
        }
        int res = whiteipMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        if(redisUtils.hasKey(NoahEnum.WHITEIPDIR.getCode()+whiteip.getWhiteip())){
            redisUtils.del(NoahEnum.WHITEIPDIR.getCode()+whiteip.getWhiteip());
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
